<script setup lang="ts">
import { ref } from "vue";
import { usePlan } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "Plan"
});

const formRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  addDtoRef,
  pagination,
  selectTemplate,
  templateList,
  loadingTitle,
  updatePlanDto,
  TemplateVisible,
  updatePanelVisible,
  rules,
  onSearch,
  HandSearch,
  // resetForm,
  handleUpdate,
  handleDelete,
  closeTemplate,
  HandTemplate,
  HandleSvaTemplate,
  handleSizeChange,
  ClosePanelVisible,
  handleCurrentChange,
  addPlan
} = usePlan();
</script>

<template>
  <div class="main">
    <el-form ref="formRef" :inline="true" :model="form" class="bg-bg_color w-[99/100] pl-4 pt-4">
      <el-form-item label="计划名称" prop="OrganizationName">
        <el-input v-model="form.PlanName" clearable placeholder="请输入计划名称" class="!w-[180px]" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="useRenderIcon(Search)" :loading="loading" @click="onSearch(form.PlanName)">
          搜索
        </el-button>
        <!-- <el-button :icon="useRenderIcon(Refresh)" @click="resetForm()">
          重置
        </el-button> -->
      </el-form-item>
    </el-form>

    <el-dialog @close="ClosePanelVisible(addDtoRef)" :close-on-click-modal="false" v-model="updatePanelVisible"
      :title="loadingTitle" width="30%" center>
      <el-form ref="addDtoRef" label-position="left" label-width="100px" :rules="rules" :model="updatePlanDto"
        style="max-width: 460px">
        <el-form-item label="计划名称" prop="PlanName">
          <el-input v-model="updatePlanDto.PlanName" class="w-192" />
        </el-form-item>
        <el-form-item label="计划类型" prop="Type">
          <el-select v-model="updatePlanDto.Type" class="w-192" :disabled="updatePlanDto.isDisableEdit">
            <el-option label="实训任务" :value="1" />
            <el-option label="竞赛任务" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否为模板" prop="isTemplate">
          <el-radio-group v-model="updatePlanDto.isTemplate" :disabled="updatePlanDto.isTemplates ? true : false">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否 </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="Remarks">
          <el-input type="textarea" v-model="updatePlanDto.Remarks" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="addPlan(addDtoRef)"
            :disabled="updatePlanDto.isTemplates ? !(updatePlanDto.isEdit == 1) : false">
            保存
          </el-button>
          <el-button @click="ClosePanelVisible(addDtoRef)">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 上传模板 -->
    <el-dialog :close-on-click-modal="false" v-model="TemplateVisible" title="导入模板" width="20%" center>
      <el-form ref="TemplateRef" label-position="left" label-width="100px">
        <el-form-item label="计划名称">
          <el-select class="w-192" v-model="selectTemplate">
            <el-option v-for="item in templateList" :label="item.selectName" :value="item.selectId" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="HandleSvaTemplate()"> 保存 </el-button>
          <el-button @click="closeTemplate()">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <PureTableBar title="计划列表" @refresh="onSearch" class="h100-60">
      <template #buttons>
        <p class="text-red-500 absolute left-[120px] leading-7 text-sm">注：双击查看详情</p>
        <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="HandTemplate()">
          导入模板
        </el-button>
        <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="handleUpdate(undefined)">
          添加计划
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table border align-whole="center" showOverflowTooltip table-layout="auto" :loading="loading" :size="size"
          :data="dataList" :columns="columns" :checkList="checkList" :pagination="pagination" highlight-current-row
          :paginationSmall="size === 'small' ? true : false" :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }" @size-change="handleSizeChange" @current-change="handleCurrentChange" @cell-click="HandSearch"
          @cell-dblclick="handleUpdate">
          <template #operation="{ row }">
            <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
              @click="handleUpdate(row)" :disabled="row.isTemplate ? !(row.isEdit == 1) : false">
              修改
            </el-button>
            <el-button class="reset-margin" link type="danger" @click="handleDelete(row)" :size="size"
              :icon="useRenderIcon(Delete)" :disabled="row.isTemplate ? !(row.isDelete == 1) : false">
              删除
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
